package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
@Api(tags = "检查组模块")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;


    @ApiOperation("检查组模块-新增检查组")
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-添加]data:{},checkitemIds:{}", checkGroup, checkitemIds);
        checkGroupService.add(checkGroup, checkitemIds);
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }


    @ApiOperation("检查组模块-检查组分页查询")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data:{}", queryPageBean);
        //查询结果
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,pageResult);

    }

    @ApiOperation("检查组模块-查询检查组详情")
    @GetMapping("/findById")
    public Result findById(Integer id) {
        log.info("[检查组-根据id查询]id:{}", id);

        //调用业务处理
        CheckGroup checkGroup = checkGroupService.findById(id);
        //封装返回值
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroup);

    }

    @ApiOperation("检查组模块-查询检查项ID集合")
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查组-根据检查组ID查询关联的检查项ID集和]id:{}", id);

        //调用业务处理
        List<Integer> itemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        //封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, itemIds);

    }

    @ApiOperation("检查组模块-更新检查组")
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-编辑]data:{},checkitemIds:{}", checkGroup, checkitemIds);

        checkGroupService.edit(checkGroup, checkitemIds);
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @ApiOperation("检查组模块-删除检查组")
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id){
        try {
            checkGroupService.delete(id);
            return  new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConst.DELETE_CHECKGROUP_FAIL);
        }

    }


    @ApiOperation("检查组模块-查询全部检查组")
    @GetMapping("/findAll")
    public Result findAll() {
        //rpc调用查询数据
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        //封装返回结果
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroups);
    }

}